package com.tigerlee.candle.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tigerlee.candle.R;
import com.tigerlee.candle.util.SharedPreference;

public class CustomDialogEditActivity extends Activity implements
		OnClickListener {

	private String mHint = null;
	private String mName = null;
	private EditText mEditText;
	private TextView mOkButton;
	private TextView mCancelButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// No Title Bar
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.custom_dialog_edit);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setupView();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// Skip back key when showing intro activity
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return false;
		return super.onKeyDown(keyCode, event);
	}

	public void setupView() {
		mEditText = (EditText) findViewById(R.id.edit_custom);
		mOkButton = (TextView) findViewById(R.id.ok_text_custom_button);
		mCancelButton = (TextView) findViewById(R.id.cancel_text_custom_button);

		mName = getNamePreference();
		mHint = getString(R.string.title_chatname);
		if (mHint != null) {
			if (mHint.equals("") == false) {
				mEditText.setHint(mHint);
			}
		}
		if (mName != null) {
			if (mName.equals("") == false) {
				mEditText.setText(mName);
			}
		}

		// set & return color to default
		mOkButton.setBackgroundColor(Color.BLACK);
		mOkButton.setTextColor(Color.WHITE);
		mCancelButton.setBackgroundColor(Color.BLACK);
		mCancelButton.setTextColor(Color.WHITE);

		mOkButton.setOnClickListener(this);
		mCancelButton.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ok_text_custom_button:
			v.setBackgroundColor(Color.WHITE);
			mOkButton.setTextColor(Color.BLACK);
			String mName = mEditText.getText().toString();
			if (mName == null || mName.equals("") ==true) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.toast_fail_get_nickname),
						Toast.LENGTH_SHORT).show();
				setupView();

			} else {
				setNamePreference(mName);
				setResult(RESULT_OK);
				finish();
			}
			break;
		case R.id.cancel_text_custom_button:
			v.setBackgroundColor(Color.WHITE);
			mCancelButton.setTextColor(Color.BLACK);
			setResult(RESULT_CANCELED);
			finish();
			break;
		default:
			break;
		}
	}

	public void setNamePreference(String chatname) {
		SharedPreference mSharedPreference = new SharedPreference(
				getApplicationContext());
		mSharedPreference.setChatname(chatname);
	}
	public String getNamePreference(){
		SharedPreference mSharedPreference = new SharedPreference(
				getApplicationContext());
		return mSharedPreference.getChatname();
	}

}
